// pages/score/score.js
const memberService = require('../../../../api/memberService.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scoreList: [],
    memberScoreBalabce: 0,
    mobile: '',
    page: 1,
    pageSize: 10,
    pageCount: 1,
    noMore: false,
    loading: false,
    loadingText: '正在加载...'
  },
  // 检查当前用户是否登录授权
  checkAuth: function () {
    try {
      if (!app.globalData.userInfo) {
        return false;
      } else {
        return true;
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  // 获取积分余额
  getScoreBalance: function () {
    memberService.getBalance(this.data.mobile).then(res => {
      console.log(res)
      this.setData({
        memberScoreBalabce: res
      })
    }).catch(e => {
      wx.showToast({
        title: e.message,
        icon: 'none',
        duration: 2000
      })
    })
  },

  // 操作记录
  handleAction: function (value) {
    if (value === 'recharge') {
      return '充值'
    } else if (value === 'consume') {
      return '消费'
    } else if (value === 'adjust') {
      return '调整'
    } else if (value === 'checkIn') {
      return '签到'
    } else if (value === 'refund') {
      return '退款'
    } else {
      return value
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    const that = this
    if (app.globalData.userInfo) {
      if (app.globalData.userInfo.member) {
        that.setData({
          mobile: app.globalData.userInfo.member.mobile
        })
        this.getScoreBalance()
        that.handelQueryList(this.data.page);
      } else {
        wx.showToast({
          title: '您还不是会员哦，请绑定手机号成为会员~',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      wx.showToast({
        title: '您还没有登陆，请登录后查询积分明细~',
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      scoreList: [],
      mobile: '',
      page: 1,
      pageCount: 1,
      noMore: false
    })
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page < this.data.pageCount) {
      // 如果查询结果数量不为0，说明还有下一页，执行查询操作
      this.setData({
        loading: true
      })
      let page = this.data.page;
      ++page;
      this.handelQueryList(page);
    } else {
      // 没有下一页，提示已经查询完毕
      if (this.data.scoreList.length > 0) {
        this.setData({
          noMore: true
        })
      } else {
        wx.showToast({
          title: '没有更多数据~',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
   * 调用接口获取积分明细
   */
  handelQueryList: function (page) {
    const that = this
    let postData = {
      memberMobileEquals: this.data.mobile,
      page: page,
      pageSize: this.data.pageSize,
      orderDirection: 'desc'
    }
    memberService.queryHst(postData).then(res => {
      console.log(res)
      let tempScoreList = this.data.scoreList
      if (res.records && res.records.length > 0) {
        res.records.forEach(item => {
          let tempItem = {}
          if (item.occur > 0) {
            let occur = '+' + item.occur
            tempItem = {
              ...item,
              occur: occur,
              increase: true
            }
          } else if (item.occur < 0) {
            let occur = 0 - item.occur
            occur = '-' + occur
            tempItem = {
              ...item,
              occur: occur,
              increase: false
            }
          } else {
            tempItem = {
              ...item,
              occur: '-0.00',
              increase: false
            }
          }
          tempItem = {
            ...tempItem,
            action: item.remark,
            // action: that.handleAction(item.action)
          }
          tempScoreList.push(tempItem)
        });
        let pageCount = parseInt(res.recordCount / res.pageSize) + 1;
        this.setData({
          page: res.page,
          pageCount: pageCount,
          scoreList: tempScoreList
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    })
  }
})